package com.yf.dxapp.module.app.service.teacher;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.yf.dxapp.module.app.controller.admin.teacher.vo.TeacherPageReqVO;
import com.yf.dxapp.module.app.controller.admin.teacher.vo.TeacherSaveReqVO;
import com.yf.dxapp.module.app.dal.dataobject.teacher.TeacherDO;
import com.yf.dxapp.module.app.dal.mysql.teacher.TeacherMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yf.dxapp.module.app.enums.ErrorCodeConstants.DATA_NOT_EXISTS;

/**
 * 老师信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TeacherServiceImpl implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    @Override
    public Long createTeacher(TeacherSaveReqVO createReqVO) {
        // 插入
        TeacherDO teacher = BeanUtils.toBean(createReqVO, TeacherDO.class);
        teacherMapper.insert(teacher);
        // 返回
        return teacher.getId();
    }

    @Override
    public void updateTeacher(TeacherSaveReqVO updateReqVO) {
        // 校验存在
        validateTeacherExists(updateReqVO.getId());
        // 更新
        TeacherDO updateObj = BeanUtils.toBean(updateReqVO, TeacherDO.class);
        teacherMapper.updateById(updateObj);
    }

    @Override
    public void deleteTeacher(Long id) {
        // 校验存在
        validateTeacherExists(id);
        // 删除
        teacherMapper.deleteById(id);
    }

    private void validateTeacherExists(Long id) {
        if (teacherMapper.selectById(id) == null) {
            throw exception(DATA_NOT_EXISTS);
        }
    }

    @Override
    public TeacherDO getTeacher(Long id) {
        return teacherMapper.selectById(id);
    }

    @Override
    public PageResult<TeacherDO> getTeacherPage(TeacherPageReqVO pageReqVO) {
        return teacherMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TeacherDO> getTeacherByList(List<Long> ids) {
        return teacherMapper.selectList(TeacherDO::getId, ids);
    }

}